home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 359_12 / patch5.001 / LIBSRC / C / LIB / MODF.C
Encoding:
C/C++ Source or Header  |  1991-09-11  |  2.2 KB  |  58 lines

  1. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  2. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  3. ** Rochester NH, 03867-2954, USA.
  4. */
  5.  
  6. /*-
  7.  * Copyright (c) 1990 The Regents of the University of California.
  8.  * All rights reserved.
  9.  *
  10.  * This code is derived from software contributed to Berkeley by
  11.  * Sean Eric Fagan
  12.  *
  13.  * Redistribution and use in source and binary forms are permitted
  14.  * provided that: (1) source distributions retain this entire copyright
  15.  * notice and comment, and (2) distributions including binaries display
  16.  * the following acknowledgement:  ``This product includes software
  17.  * developed by the University of California, Berkeley and its contributors''
  18.  * in the documentation or other materials provided with the distribution
  19.  * and in all advertising materials mentioning features or use of this
  20.  * software. Neither the name of the University nor the names of its
  21.  * contributors may be used to endorse or promote products derived
  22.  * from this software without specific prior written permission.
  23.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  24.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  25.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  26.  */
  27.  
  28. #if defined(LIBC_SCCS) && !defined(lint)
  29. static char sccsid[] = "@(#)modf.c    5.1 (Berkeley) 4/23/90";
  30. #endif /* LIBC_SCCS and not lint */
  31.  
  32. /*
  33.  * modf(value, iptr): return fractional part of value, and stores the
  34.  * integral part into iptr (a pointer to double).
  35.  *
  36.  * Written by Sean Eric Fagan (sef@kithrup.COM)
  37.  * Sun Mar 11 20:27:30 PST 1990
  38.  */
  39.  
  40. /* With CHOP mode on, frndint behaves as TRUNC does.  Useful. */
  41. double
  42. modf(double value, double *iptr)
  43. {
  44.     double temp;
  45.     volatile short i87flag, i87temp;
  46.     __asm ("fnstcw %0" : "=m" (i87flag) : );
  47.     __asm ("fwait");
  48.     i87temp = i87flag | 0xc00 ; /* turn on chop mode [truncation] */
  49.     __asm ("fldcw %0" : : "m" (i87temp));
  50.     __asm ("fwait");
  51.     __asm ("frndint" : "=f" (temp) : "0" (value)); /* temp = int of value */
  52.     __asm ("fwait");
  53.     __asm ("fldcw %0" : : "m" (i87flag));
  54.     __asm ("fwait");
  55.     *iptr = temp;
  56.     return (value - temp);
  57. }
  58.